package test;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class InitLiveview {

	/**
	 * @param args
	 */
	//public static void main(String[] args) {
	//	new TestReader();
	//}

	public static void main(String[] args) {
        String requestUrl = "http://localhost:8090/?action=stream";
        BufferedImage image = null;
        try {
            URL url = new URL(requestUrl.toString());
            InputStream istream = url.openStream();
            //BufferedReader in = new BufferedReader(new InputStreamReader(istream));
            System.out.println("-----RESPONSE START-----");
            int readByte;
            String line = "";
            for (int i = 0; i < 2; i++){
            	while ((readByte = istream.read()) != 13) {
            		line += (char) readByte;
            	}
            	istream.read();
            	System.out.println("First: " + line);
            	line = "";
            }
            
            while ((readByte = istream.read()) != 13) {
            	line += (char) readByte;
            }
            istream.read();
            int length = Integer.parseInt(line.split(" ")[1]);
            System.out.println("Image size: " + length);
            line = "";
            
            while ((readByte = istream.read()) != 13) {
            	line += (char) readByte;
            }
            istream.read();
            System.out.println(line);
            line = "";
            /*
            while ((readByte = istream.read()) != 13) {
            	line += (char) readByte;
            }
            istream.read();
            System.out.println("There:" + line);
            line = "";
            */
            int bytesRead = 0;
            int currentBytes = 0;
            byte[] byteArray = new byte[length];
            while (bytesRead != length) {
            	currentBytes = istream.available();
            	currentBytes = (currentBytes+bytesRead > length) ? length - bytesRead : currentBytes;
            	byte[] currentByteArray = new byte[currentBytes];
                istream.read(currentByteArray);
                System.arraycopy(currentByteArray, 0, byteArray, bytesRead, currentBytes);
                bytesRead += currentBytes;
            }
            
            while ((readByte = istream.read()) != 13) {
            	line += (char) readByte;
            }
            istream.read();
            System.out.println("And here:" + line);
            line = "";
            
            InputStream in = new ByteArrayInputStream(byteArray);
        	image = ImageIO.read(in);
        	
            
        	JFrame frame = new JFrame();
        	JPanel panel = new JPanel();
        	JLabel label = null;
            if (image != null) {
                label = new JLabel(new ImageIcon(image));
                frame.getContentPane().add(panel, BorderLayout.CENTER);
                panel.add(label);
                frame.pack();
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
            //in.close();
            System.out.println("-----RESPONSE END-----");
            
            while (true) {
                line = "";
                for (int i = 0; i < 2; i++){
                	while ((readByte = istream.read()) != 13) {
                		line += (char) readByte;
                	}
                	istream.read();
                	System.out.println("First: " + line);
                	line = "";
                }
                
                while ((readByte = istream.read()) != 13) {
                	line += (char) readByte;
                }
                istream.read();
                length = Integer.parseInt(line.split(" ")[1]);
                System.out.println("Image size: " + length);
                line = "";
                
                while ((readByte = istream.read()) != 13) {
                	line += (char) readByte;
                }
                istream.read();
                System.out.println(line);
                line = "";
                /*
                while ((readByte = istream.read()) != 13) {
                	line += (char) readByte;
                }
                istream.read();
                System.out.println("There:" + line);
                line = "";
                */
                bytesRead = 0;
                currentBytes = 0;
                byteArray = new byte[length];
                while (bytesRead != length) {
                	currentBytes = istream.available();
                	currentBytes = (currentBytes+bytesRead > length) ? length - bytesRead : currentBytes;
                	byte[] currentByteArray = new byte[currentBytes];
                    istream.read(currentByteArray);
                    System.arraycopy(currentByteArray, 0, byteArray, bytesRead, currentBytes);
                    bytesRead += currentBytes;
                }
                
                while ((readByte = istream.read()) != 13) {
                	line += (char) readByte;
                }
                istream.read();
                System.out.println("And here:" + line);
                line = "";
                
                in = new ByteArrayInputStream(byteArray);
            	image = ImageIO.read(in);
            	//label = new JLabel(new ImageIcon(image));
            	//panel.removeAll();
            	//panel.add(label);
            	((ImageIcon) label.getIcon()).setImage(image);
            	panel.updateUI();
            	
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
